package com.easybuilder.common.base.controller;

import com.easybuilder.common.base.entity.BaseEntity;
import com.easybuilder.common.base.entity.SearchEntity;
import com.easybuilder.common.base.service.BaseService;
import com.easybuilder.common.base.service.PageQuery;
import com.easybuilder.common.utils.StringUtil;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;


/**
 * @author ZXS
 * @version 1.0
 * @description 类
 * @date 2021-5-25 22:40
 */
@RestController
@RequestMapping("/base")
public class BaseController {
    @Autowired
    private PageQuery pageQuery;
    @Autowired
    private BaseService baseService;


    /***
     * @description: 默认分页查询
     * @param:
     * @param searchEntity 查询条件对象
     * @return:
     * @author ZXS
     * @date: 2021-5-29 9:28
     */
    @PostMapping("/pageQuery")
    public Result<Object> pageQuery(@RequestBody SearchEntity searchEntity) {
        String tableName = searchEntity.getTableName();
        if (StringUtil.isEmpty(tableName)) {
            return Error("表名不能为空!");
        }
        PageQuery.PageResult page = pageQuery.getPage(searchEntity);
        return OK(page);
    }


    @PostMapping("/doSave")
    public Result<Object> doSave(@RequestBody BaseEntity entity) {
        String tableName = entity.getTableName();
        if (StringUtil.isEmpty(entity.getTableName())) {
            return Error("表名称不能为空!");
        }
        String id = entity.getEntity().getString("id");
        String message = StringUtil.isEmpty(id) ? "新建成功!" : "保存成功!";
        entity =  baseService.doSave(entity);
        return OK(entity.getEntity(), message);
    }

    @PostMapping("delete")
    public Result<Object> delete(@RequestBody BaseEntity entity) {
        if (StringUtil.isEmpty(entity.getTableName())) {
            return Error(HttpStatus.BAD_REQUEST, "tableName不能为空!");
        }
        if (entity.getEntity() == null || StringUtil.isEmpty(entity.getEntity().getString("ids"))) {
            return Error(HttpStatus.BAD_REQUEST, "请至少勾选一条数据!");
        }
        long delete = baseService.delete(entity);
        return OK(null, "成功删除" + delete + "条数据!");
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @ToString
    public static class Result<T> implements Serializable {
        //响应码
        private Integer status;
        //是否成功
        private boolean success;
        //返回值
        private T data;
        //提示信息
        private String message;
    }

    //正确响应
    public static  <T> Result OK(T data, String... message) {
        String msg = message != null && message.length > 0 ? message[0] : "";
        Result result = new Result(HttpStatus.OK.value(), true, data, msg);
        return result;
    }

    //错误响应
    public <T> Result<T> Error(String message) {
        message =  StringUtil.isEmpty(message) ? "服务器繁忙，请稍后重试" : message;
        Result result = new Result(HttpStatus.INTERNAL_SERVER_ERROR.value(), false, null, message);
        return result;
    }
    //错误响应
    public <T> Result<T> Error(HttpStatus status, String message) {
        message =  StringUtil.isEmpty(message) ? "服务器繁忙，请稍后重试" : message;
        Result result = new Result(status.value(), false, null, message);
        return result;
    }

    //参数异常
    public Result BadRequest() {
        return new Result(HttpStatus.BAD_REQUEST.value(), false, null, "参数异常");
    }


    //获取response
    private HttpServletResponse getResponse(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletResponse response = attributes.getResponse();
        return response;
    }

    //获取request
    private HttpServletRequest getRequest(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        return request;
    }

}
